package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.OrderSettingDao;
import com.itheima.pojo.OrderSetting;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;


@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {
    @Autowired
    private OrderSettingDao orderSettingDao;

    @Override
    public void add(List<OrderSetting> orderSettingList) {
        if (orderSettingList != null && orderSettingList.size() > 0) {
            for (OrderSetting orderSetting : orderSettingList) {
                //去数据库中查询预约日期是否存在
                Integer count = orderSettingDao.findOrderDate(orderSetting.getOrderDate());
                //如果存在则更新
                if (count > 0) {
                    orderSettingDao.updateNumber(orderSetting);
                } else {
                    orderSettingDao.addOrderSetting(orderSetting);
                }

                //不存在就插入
            }
        }
    }

    @Override
    public List<Map> getOrderSettingByMonth(String date) {
        List<Map> data = null;
        if (date != null & date.length() > 0) {
            String begin = date + "-1";
            String end = date + "-31";
            Map map = new HashMap();
            map.put("dateBegin", begin);
            map.put("dateEnd", end);
            List<OrderSetting> orderSettingList = orderSettingDao.getOrderSettingByMonth(map);
            data = new ArrayList<>();
            for (OrderSetting orderSetting : orderSettingList) {
                Map orderSettingMap = new HashMap();
                orderSettingMap.put("date", orderSetting.getOrderDate().getDate());
                orderSettingMap.put("number", orderSetting.getNumber());
                orderSettingMap.put("reservations", orderSetting.getReservations());
                data.add(orderSettingMap);
            }
        }
        return data;
    }

    @Override
    public void editNumberBydate(OrderSetting orderSetting) {
        Integer count = orderSettingDao.findOrderDate(orderSetting.getOrderDate());
        if (count >0){
            orderSettingDao.editNumberBydate(orderSetting);
        }else {
            orderSettingDao.addOrderSetting(orderSetting);
        }

    }
}
